package samsung.sds.systemDictManagement.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.systemDictManagement.dao.SystemDictManagementMapper;
import samsung.sds.systemDictManagement.model.SystemDictEntity;
import samsung.sds.systemDictManagement.service.SystemDictManagementService;
/**
 * @author tingcai.liu
 * @createDate 2016-02-03
 * 
 * 数据字典的 Service的接口的实现
 */
@Service
public class SystemDictManagementServiceImpl extends AbstractBaseService<SystemDictEntity> implements
		SystemDictManagementService {
	
	@Autowired
	private SystemDictManagementMapper systemDictMapper;

	@Override
	protected BaseMapper<SystemDictEntity> getMapper() {
		return systemDictMapper;
	}
	
	/**
	 * 新增或修改数据字典对象
	 * @param optype
	 * @param systemDict
	 * @return
	 */
	public int addOrUpdateSystemDict(String optype, SystemDictEntity systemDict) {
		//检查传递的参数是否为空
		if(checkParameterIsNull(optype) || checkParameterIsNull(systemDict)) {
			return 0;
		}
		//新建数据字典对象
		if(optype.equalsIgnoreCase("create")){
			int isCreateSuccess = super.insertEntity(systemDict);
			return isCreateSuccess;
			
		//修改数据字典对象		
		}else if(optype.equalsIgnoreCase("update")){
			return updateSystemDict(systemDict);
		}
		return 0;
	}

	/**
	 * 修改数据字典对象
	 * @param systemDict
	 * @return
	 */
	private int updateSystemDict(SystemDictEntity systemDict) {
		//判断是否修改了groupId
		if(!systemDict.getOldGroupId().equals(systemDict.getGroupId())) {
			return updateGroupId(systemDict);
		}else {
			return this.updateEntity(systemDict);
		}
	}

	/**
	 * 修改了组名
	 * @param systemDict
	 * @return
	 */
	private int updateGroupId(SystemDictEntity systemDict) {
		if(systemDictMapper.updateEntity(systemDict) == 1 ) {
			//修改其余几条记录的groupId
			systemDictMapper.updateSYS_DICT(systemDict);
			return 1;
		}
		return 0;
	}
	
	/**
	 * 判断传递的参数是否为空
	 * @param parameter
	 * @return
	 */
	private boolean checkParameterIsNull(Object parameter) {
		return (parameter == null) ? true : false;
	}
	
	/**
	 * 删除项
	 * @param systemDict
	 * @return
	 */
	public int deleteSystemDict(SystemDictEntity systemDict) {
		return this.deleteEntity(systemDict);
	}
	
	/**
	 * 删除组
	 * @param systemDict
	 * @return
	 */
	public int deleteSystemDictByGroupId(SystemDictEntity systemDict) {
		//该组下的记录数量
		int updateCount = systemDictMapper.selectSYS_DICT01(systemDict);
		
		int isDel = systemDictMapper.deleteSYS_DICT(systemDict);
		
		return (updateCount == isDel) ? 1 : 0;
	}

	/**
	 * 检查optionKey是否重复
	 * @param parammeters
	 * @return
	 */
	public boolean checkOptionKeyRepeat(String parammeters) {
		//将从前台获取的参数组合进行拆分
		String[] optionKeyStution = parammeters.split(",");
		String optionKeyNow = optionKeyStution[0];
		String groupIdNow = optionKeyStution[1];
		String groupIdOld = optionKeyStution[2];
		String optionKeyOld = optionKeyStution[3];
		String optype = optionKeyStution[4];
		
		SystemDictEntity systemDict = new SystemDictEntity();
		systemDict.setGroupId(groupIdNow);
		systemDict.setOptionKey(optionKeyNow);
		systemDict.setOldGroupId(groupIdOld);
		systemDict.setOldOptionKey(optionKeyOld);
				
		//根据修改前后的groupId,查询得到结果集
		List<SystemDictEntity> systemDictList = systemDictMapper.findDictListByProperty(systemDict);
		if(systemDictList != null && systemDictList.size() > 0){
			if("create".equals(optype)){
				return false;
			}else{
				//如果没修改GroupId
				if(groupIdOld.equals(groupIdNow)){
					return checkOptionKey(optionKeyOld, systemDictList);
				}else{
					//判断修改的groupId在数据库中是否存在
					boolean isRepeatGroup = checkGroupIdRepeat(systemDict);
					//如果存在
					if(isRepeatGroup){
						return checkGroupIdAndOptionKey(groupIdNow, groupIdOld);
					}else{
						//获取原来group的记录，判断是否有与当前optionKey重复的记录
						SystemDictEntity oldGroupDict = new SystemDictEntity();
						oldGroupDict.setGroupId(groupIdNow);
						oldGroupDict.setOldGroupId(groupIdOld);
						List<SystemDictEntity> oldSystemDictList = systemDictMapper.findDictListByProperty(systemDict);
						
						//如果修改了optionKey
						if(!optionKeyOld.equals(optionKeyNow) && checkOptionKey(optionKeyNow, oldSystemDictList)) {
							return false;
						}
						return true;
					}
				}
			}
		}else{
			if(!groupIdNow.equals(groupIdOld) && !optionKeyNow.equals(optionKeyOld) && "update".equals(optype)) {
				//获取原来group的记录，判断是否有与当前optionKey重复的记录
				SystemDictEntity oldGroupDict = new SystemDictEntity();
				oldGroupDict.setGroupId(groupIdNow);
				oldGroupDict.setOldGroupId(groupIdOld);
				return checkGroupIdAndOptionKey(groupIdNow, groupIdOld);
			}
			return true;
		}
	}

	/**
	 * 在groupId被修改前后检查optionKey是否重复
	 * @param optionKey
	 * @param systemDictList
	 * @return
	 */
	private boolean checkOptionKey(String optionKey,
			List<SystemDictEntity> systemDictList) {
		boolean flag = false;
		//遍历结果集
		for (SystemDictEntity entity : systemDictList) {
			if(entity.getOptionKey().equals(optionKey)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断在groupId被修改前后，是否存在相同的optionKey
	 * @param groupIdNow
	 * @param groupIdOld
	 * @return
	 */
	private boolean checkGroupIdAndOptionKey(String groupIdNow,
			String groupIdOld) {
		SystemDictEntity dict = new SystemDictEntity();
		dict.setGroupId(groupIdNow);
		dict.setOldGroupId(groupIdOld);
		//判断该组下是否有optionKey
		return (systemDictMapper.selectSYS_DICT03(dict) == systemDictMapper.selectSYS_DICT04(dict)) ? true : false;
	}
	
	/**
	 * 检查groupId是否已经存在是否重复
	 * @param systemDict
	 * @return
	 */
	public boolean checkGroupIdRepeat(SystemDictEntity systemDict) {
		int groupIdNumber = systemDictMapper.selectSYS_DICT01(systemDict);
		if(groupIdNumber >= 1) {
			return true;
		}
		return false;
	}

	/**
	 * 通过groupId获取数据字典的集合
	 * @param gruopId
	 * @return
	 */
	@Override
	public List<SystemDictEntity> getSystemDictList(String groupId) {
		return systemDictMapper.selectSYS_DICT02(groupId);
	}
}
